<template>
  <div>
    <!-- 新增,编辑的弹出框 -->
    <el-dialog title="新增信息" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="用户名：" label-width="180px">
          <el-input v-model="formData.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码：" label-width="180px">
          <el-input v-model="formData.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" label-width="180px">
          <el-input v-model="formData.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收货地址" label-width="180px">
          <el-input v-model="formData.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收件人：" label-width="180px">
          <el-input v-model="formData.consignee" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注：" label-width="180px">
          <el-input v-model="formData.remarks" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="是否启用：" label-width="180px" v-if="showEagleMap" disabled="disabled">
          <el-input v-model="formData.isactived" autocomplete="off"></el-input>
        </el-form-item>-->
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input placeholder="需要搜索的id" v-model="select.id"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="selectId">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="需要搜索的用户名"
            v-model="select.username"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="selectUsername">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-table :data="tableData" style="width: 100% background:#545c64" border>
      <el-table-column
        type="index"
        label="序号"
        width="60"
        :index="indexMethod"
        align="center"
      />

      <el-table-column label="Id">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.isActived }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createdTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="电话">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货地址">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收件人">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.consignee }}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.remarks }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="open(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getList, Add, Update, Delete, getById, Username } from "../api/users";
export default {
  data() {
    return {
      select: {
        id: "",
        username: "",
      },
      //v-if隐藏输入框
      showEagleMap: true,
      //绑定的数据
      tableData: [],

      //分页
      pager: {
        pageIndex: 1, //页码
        pageSize: 10, //页面大小
        rowsTotal: 30, //总条数
      },
      //增加，编辑返回的数据
      formData: {
        id: 0,
        username: "",
        remarks: "",
        password: "113",
        phone: "",
        address: "",
        consignee: "",
        // isactived:''
      },
      //增加，编辑的弹出框
      dialogFormVisible: false,
    };
  },
  methods: {
    //搜索用户名
    selectUsername() {
      console.log(this.select.username);
      if (this.select.username != "") {
        Username(this.select.username, this.pager)
          .then((data) => {
            if (data.data.length !== 0) {
              (this.tableData = []), console.log(this.tableData);
              var res = data.data.data;
              console.log(res);
              res.forEach((item) => {
                this.tableData.push(item);
              });

              console.log(this.tableData);

              this.pager = data.data.pager;
            } else {
              this.$message({
                showClose: true,
                message: "查询不到此用户名",
                type: "warning",
              });
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.fetchData(this.pager);
      }
    },
    //搜索id
    selectId() {
      if (this.select.id != "") {
        getById(this.select.id)
          .then((data) => {
            if (data.data !== null) {
              console.log(this.select.id);

              this.tableData = [];
              this.tableData.push(data.data);
              console.log(data);
              this.pager.rowsTotal = 1;
            } else {
              this.$message({
                showClose: true,
                message: "查询不到此id",
                type: "warning",
              });
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.fetchData(this.pager);
      }
    },
    //序号列随便页面的变化而变化
    indexMethod(index) {
      index = index + 1 + (this.pager.pageIndex - 1) * this.pager.pageSize;
      return index;
    },

    //弹出框取消按钮
    handleCancel() {
      this.dialogFormVisible = false;
    },
    //增加按钮
    handleAdd() {
      this.showEagleMap = false;
      (this.dialogFormVisible = true),
        (this.formData.id = 0),
        (this.formData.username = ""),
        (this.formData.remarks = "");
      console.log(this.formData);
      console.log(this.formData.id);
    },
    //弹出框确认按钮的判断
    handleSave() {
      this.dialogFormVisible = false;
      //判断id=0的话新增，否则编辑
      if (this.formData.id == 0) {
        if (this.formData.username != "") {
          Add(this.formData).then(({ data }) => {
            console.log(data);
            this.tableData.push(data);
            this.fetchData(this.pager);
            this.formData.username = "";
            this.formData.password = "";
            this.formData.phone = "";
            this.formData.address = "";
            this.formData.consignee = "";
            this.formData.remarks = "";
            this.$message({
              showClose: true,
              message: "恭喜你，新增成功",
              type: "success",
            });
          });
        } else {
          this.$message.error("添加失败，请填写用户名！")
        }
      } else {
        Update(this.formData.id, this.formData).then(({ data }) => {
          console.log(data);
          this.tableData.splice(this.Index, 1, data);

          this.$message({
            showClose: true,
            message: "恭喜你，编辑成功",
            type: "success",
          });
        });
      }
    },
    //编辑按钮触发的事件
    handleEdit(index, row) {
      this.showEagleMap = false;
      console.log(index, row);
      (this.dialogFormVisible = true),
        (this.formData.id = row.id),
        (this.formData.username = row.username),
        (this.formData.phone = row.phone),
        (this.formData.consignee = row.consignee),
        (this.formData.address = row.address),
        (this.formData.remarks = row.remarks);
      //编辑目标的下标
      this.Index = index;

      // this.formData.isactived=String(row.isActived)
      // console.log(this.from.isactived);
    },
    //删除按钮
    // handleDelete(index, row) {
    //   console.log(index, row);
    //   Delete(row.id).then(({data})=>{
    //     console.log(data);

    //   })
    // },

    //页面条数修改后重新拉取数据
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pager.pageSize = val;
      this.fetchData(this.pager);
    },
    //页面页数修改后重新拉取数据
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pager.pageIndex = val;

      this.fetchData(this.pager);
    },
    //拉取数据方法
    fetchData(pager) {
      getList(pager)
        .then((data) => {
          // console.log(data);
          let res = data.data;
          console.log(res.data);
          this.pager = res.pager;
          this.tableData = res.data;
          // console.log(res.pager);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //删除按钮、提示框
    open(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          Delete(row.id).then(({ data }) => {
            console.log(data);
            this.tableData.splice(index, 1);
            console.log(this.pager.pageIndex);
            this.fetchData(this.pager);
            //如果第二页删空则跳回第一页
            if (index == 0) {
              let newIndex = this.pager.pageIndex - 1;
              this.handleCurrentChange(newIndex);
              this.fetchData(this.pager);
            }
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    // getById(1).then(({data})=>{
    //   console.log(data);
    // }).catch(err=>{
    //   console.log(err);
    // });

    this.fetchData(this.pager);
  },
};
</script>
<style  scoped>
.el-pagination {
  padding: 15px 10px;
}
.el-card {
  margin-bottom: 20px;
}
</style>